﻿
// -----------------------------------------------------------------------
// <copyright file="ServiceCode_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ServiceCode_DAL:BaseDAL
    {

        // Lấy tất cả ServiceCode
        public static List<ServiceCode> GetAll(int shipID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.ServiceCodes);
            return DB.ServiceCodes.Where(n=>n.Ship_ID == shipID).ToList();
        }
        // Lấy ServiceCode theo Id
        public static ServiceCode GetDataById(int id)
        {
            return DB.ServiceCodes.Where(m => m.SC_ID == id).FirstOrDefault();
        }
        //Thêm một ServiceCode
        public static bool Insert(ServiceCode ServiceCode)
        {
            try
            {
                DB.ServiceCodes.InsertOnSubmit(ServiceCode);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một ServiceCode
        public static bool Delete(ServiceCode ServiceCode)
        {
            try
            {
                DB.ServiceCodes.DeleteOnSubmit(ServiceCode);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Cập nhật ServiceCode
        public static bool Update(ServiceCode ServiceCode)
        {
            try
            {
            //    ServiceCode objServiceCode = GetDataById(ServiceCode.SC_ID);
            //    objServiceCode.CodeSC = ServiceCode.CodeSC;
            //    objServiceCode.NameSC = ServiceCode.NameSC;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }

        // Kiểm tra xem ServiceCode Code có tồn tại chưa ?
        public static bool IsExits(ServiceCode ServiceCode)
        {
            ServiceCode objServiceCode = null;
            objServiceCode = DB.ServiceCodes.Where(m => (m.CodeSC == ServiceCode.CodeSC) &&
                (m.SC_ID != ServiceCode.SC_ID) && (m.Ship_ID == ServiceCode.Ship_ID)).FirstOrDefault();

            return objServiceCode != null ? true : false;
        }

        public static bool IsRelationship(ServiceCode ServiceCode)
        {
            ServiceCode serviceCode = DB.ServiceCodes.Where(n => n.SC_ID == ServiceCode.SC_ID).FirstOrDefault();
            if (null != serviceCode)
                return false;

            return true;
        }
    }
}
